package com.jk11a.shangdaast.notice;


import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jk11a.shangdaast.R;
import com.jk11a.shangdaast.dao.NoticeDao;
import com.jk11a.shangdaast.dao.UserDao;
import com.jk11a.shangdaast.user.RegistActivity;
import com.jk11a.shangdaast.user.User;

public class AddNotice extends Activity{

	private EditText et_title;
	private EditText et_content;
	private DatePicker dp_endtime;
	private RadioGroup rg_type;
	private Button btn_submit;
	private TextView tv_endtime;
	private RadioButton rb_homework;
	private RadioButton rb_contest;
	private RadioButton rb_weekly;
	private RadioButton checked;
	private Button noticeadd_reback_btn;
	NoticeBean noticeBean=new NoticeBean();
	private SharedPreferences sp=null;
	private String FILE = "saveUserNamePwd";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notice_add);
		sp = this.getSharedPreferences(FILE, Context.MODE_PRIVATE);
		et_title=(EditText)findViewById(R.id.notice_title_edit);
		et_content=(EditText)findViewById(R.id.notice_content_edit);
		rg_type=(RadioGroup)findViewById(R.id.notice_rg_type);
		rg_type.setOnCheckedChangeListener(mChangeRadio);
		dp_endtime=(DatePicker)findViewById(R.id.notice_endtime_dp);
		btn_submit=(Button)findViewById(R.id.notice_submit_btn);
		tv_endtime=(TextView)findViewById(R.id.notice_tv_endtime_show);
		 rb_homework=(RadioButton)findViewById(R.id.notice_rb_homework);
		 rb_contest=(RadioButton)findViewById(R.id.notice_rb_contest);
		 rb_weekly=(RadioButton)findViewById(R.id.notice_rb_weekly);
		 noticeadd_reback_btn=(Button)findViewById(R.id.noticeadd_reback_btn);
		 Calendar calendar=Calendar.getInstance();
	        int year=calendar.get(Calendar.YEAR);
	        int monthOfYear=calendar.get(Calendar.MONTH);
	        int dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH);
	        
	        //默认通知类型为作业，Type为2
	        noticeBean.setType(2);
	        //标题栏 返回键
	        noticeadd_reback_btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent=new Intent();
					intent.setClass(AddNotice.this, Notice.class);
					startActivity(intent);
					finish();
				}
			});
	        
	        dp_endtime.init(year, monthOfYear, dayOfMonth, new OnDateChangedListener() {
				
				public void onDateChanged(DatePicker view, int year, int monthOfYear,
						int dayOfMonth) {
					// TODO Auto-generated method stub
					tv_endtime.setText(+year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
				}
			});
	    	String titleString=et_title.getText().toString();

			
	        //提交Onclick事件
		btn_submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				noticeBean.setWriterid(sp.getInt("id", 0));
				noticeBean.setTitle(et_title.getText().toString());
				noticeBean.setMessage(et_content.getText().toString());
				noticeBean.setEndtime(tv_endtime.getText().toString());
				//还有个notice的type在ButtonGroup的触发器中设置好了
				new AddNoticeThread(AddNotice.this).start();
			}
		});
	}
	 private RadioGroup.OnCheckedChangeListener mChangeRadio = new 
	           RadioGroup.OnCheckedChangeListener()
	  {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			if(checkedId==rb_homework.getId()){
				//获取rb_homework内容
				noticeBean.setType(2);
			}
			else if (checkedId==rb_contest.getId()) {
				noticeBean.setType(3);
			}
			else if (checkedId==rb_weekly.getId()) {
				noticeBean.setType(1);
			}
		} 
	  
	  };
	private class AddNoticeThread extends Thread {
		private AddNotice addnotice2;

		public AddNoticeThread(AddNotice addnotice1) {
			addnotice2 = addnotice1;
		}	
		@Override
		public void run() {
			NoticeDao noticeDao=new NoticeDao();
			try {
				Log.i("add thread", "addnotice is called");
				Log.i("bean", noticeBean.getTitle());
				Log.i("type", Integer.toString(noticeBean.getType()));
				noticeDao.addnotice(noticeBean);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			addnotice2.handler.sendEmptyMessage(0);		
		}

	}
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				Toast.makeText(AddNotice.this, "添加成功", Toast.LENGTH_LONG)
				.show();
			
			default:
				break;
			}
		}

	};
	private void noticeadd_back(View v){
		Intent intent=new Intent();
		intent.setClass(AddNotice.this, Notice.class);
		startActivity(intent);
		finish();
	}
}
